package com.hd.assistant.biz.manager.ordermining.concurrent;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hd.assistant.dal.dao.exception.DAOException;
import com.hd.assistant.dal.dao.orderming.ItemCostsDao;
import com.hd.assistant.dal.dataobject.ordermining.ItemCost;


public class ItemInsertDBTask implements Callable<Integer>{
	private final static Logger log = LoggerFactory.getLogger(ItemInsertDBTask.class);
	private List<ItemCost> items;
	ItemCostsDao itemCostsDao;
	
	private final CountDownLatch doneSignal;  

	public ItemInsertDBTask(List<ItemCost> items,CountDownLatch doneSignal,ItemCostsDao itemCostsDao) {
		super();
		this.items = items;
		this.doneSignal = doneSignal;
		this.itemCostsDao = itemCostsDao;
	}

	
	@Override
	public Integer call() throws Exception {
		if(items == null)
			return 0;
		int total = 0;
			for(ItemCost itemCost : items){
				try {
					itemCostsDao.insertItemCost(itemCost);
					total ++;
				} catch (DAOException e) {
					log.error("ItemDBTask:"+"",e);
				}
			
			doneSignal.countDown();
		}
		
		return total;
	}

}
